function sendHttpPost() {
    cordova.plugin.http.setHeader('ceshi.tongecn.com', 'Header', 'Value');
    cordova.plugin.http.setCookie('https:/ceshi.tongecn.com', "auth=12345;SameSite=none");
    const options = {
        method: 'POST',
        data: {
            bookname: "《cordova编程》",
            author: "马弓手"
        },
        serializer:"json"
    };

    var requestId = cordova.plugin.http.sendRequest('https://ceshi.tongecn.com/member/test.jsp', options, function(response) {
        // prints 200
        console.log(response.status);
        document.getElementById("postInfo").innerHTML = response.status;
    }, function(response) {
        // prints 403
        console.log(response.status);
        //prints Permission denied
        console.log(response.error);
    });
}

function sendHttpGet() {
    cordova.plugin.http.get('http://www.tongecn.com/update/server.xml', {
        id: '12',
        message: 'test'
    }, { Authorization: 'OAuth2: token' }, function(response) {
        document.getElementById("getInfo").innerHTML = response.status;
        console.log(response.status);
    }, function(response) {
        console.error(response.error);
    });
}

function downloadHttpFile() {
    var uri = "https://cordova.apache.org/static/img/cordova_bot.png";
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        document.getElementById("downloadInfo").innerHTML = "正在下载..."
        var targetPath = dirEntry.toURL() + "cordova_bot.png";
        cordova.plugin.http.downloadFile(
            uri,
            { id: '12', message: 'test' },
            { Authorization: 'OAuth2: token' },
            targetPath,
            // success callback
            function(entry, response) {
                // prints the filename
                console.log(entry.name);
                document.getElementById("downloadInfo").innerHTML = entry.name;
                // prints the filePath
                console.log(entry.fullPath);
                // prints all header key/value pairs
                Object.keys(response.headers).forEach(function (key) {
                    console.log(key, response.headers[key]);
                });
            },
            // error callback
            function(response) {
                console.error(response.error);
            }
        );
    });
}

function uploadHttpFile() {
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        var targetPath = dirEntry.toURL() + "cordova_bot.png";
        // e.g. for single file
        //const filePath = targetPath;
        //const name = 'cordova_bot.png';

        // e.g. for multiple files 上传了两个文件
        const filePath = [dirEntry.toURL() + "cordova_bot.png", dirEntry.toURL() + "cordova_bot.png"];
        const name = ['picture', 'document'];

        cordova.plugin.http.uploadFile("https://ceshi.tongecn.com/servlet/ImageTempUpload", {
            id: '12',
            message: 'test'
        }, { Authorization: 'OAuth2: token' }, filePath, name, function (response) {
            console.log(response.status);
            document.getElementById("uploadInfo").innerHTML = response.status;
        }, function (response) {
            console.error(response.error);
        });
    });
}

function cancelUploadHttpFile() {
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        var targetPath = dirEntry.toURL() + "cordova_bot.png";
        const filePath = [dirEntry.toURL() + "cordova_bot.png", dirEntry.toURL() + "cordova_bot.png"];
        const name = ['picture', 'document'];

        var reqId = cordova.plugin.http.uploadFile("https://ceshi.tongecn.com/servlet/ImageTempUpload", {
            id: '12',
            message: 'test'
        }, { Authorization: 'OAuth2: token' }, filePath, name, function (response) {
            console.log(response.status);
        }, function (response) {
            console.error(response.status);
        });

        cordova.plugin.http.abort(reqId, function(result) {
            // prints if request was aborted: true | false
            console.log(result.aborted);
            document.getElementById("cancelUploadInfo").innerHTML = result.aborted;
        }, function(response) {
            console.error(response.error);
        });
    });
}

function cancelDownloadHttpFile() {
    cordova.plugin.http.setRequestTimeout(6.0);
    cordova.plugin.http.setReadTimeout(5.0);

    var uri = "https://cordova.apache.org/static/img/cordova_bot.png";
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        var targetPath = dirEntry.toURL() + "cordova_bot.png";
        var reqId = cordova.plugin.http.downloadFile(
            uri,
            { id: '12', message: 'test' },
            { Authorization: 'OAuth2: token' },
            targetPath,
            // success callback
            function(entry, response) {
                console.log(response.status);
            },
            // error callback
            function(response) {
                console.error(response.status);
            }
        );
        cordova.plugin.http.abort(reqId, function(result) {
            // prints if request was aborted: true | false
            console.log(result.aborted);
            document.getElementById("cancelDownloadInfo").innerHTML = result.aborted;
        }, function(response) {
            console.error(response.error);
        });

    });
}

function setServerTrustMode() {
    cordova.plugin.http.setServerTrustMode('pinned', function() {
        document.getElementById("serverTrustInfo").innerHTML = "设置成功";
    }, function(errorInfo) {
        document.getElementById("serverTrustInfo").innerHTML = errorInfo;
    });
}

function base64ToArrayBuffer(base64) {
    const binaryString = atob(base64);
    const bytes = new Uint8Array(binaryString.length);
    for (let i = 0; i < binaryString.length; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes.buffer;
}

function setClientTrustMode() {
    var p12base64 = "MIIRvwIBAzCCEXUGCSqGSIb3DQEHAaCCEWYEghFiMIIRXjCCC9IGCSqGSIb3DQEHBqCCC8Mwggu/AgEAMIILuAYJKoZIhvcNAQcBMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAhBQKYiuWkkDQICCAAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEGRD0g/IZSqKWqqgKG0uS/CAggtQP2K5/Wg95wB+lc6WcVEt7boO2UdaNjdM3jOCON3FJSXdXvGf+K44UZLBgmTiXezPNDUvbO119TYJpiipQXb7U+Ijab7KQ66+jlC8U5aiV2kOV3qZUoQzz3la1BhiT2Gp9bed39kANVTC3qkIUPWq5owz4GVwerFll7YzEL31GuMPs0eKiMvRU8IsuOZdCNQcAkB9VFrOWnnmwaoPec9g8LQsMFEsnXnKZStl38qO3ZstZ7p8GVIesrjQq68nCHEXnZ7rLxNvZ7vH9TECZ6MKDGDCP2uQXwlQ5jk5Rgti78HX3mauud7X1FySVXCsAualrxr51cVACO+kFD4vqF0EzUg6Mh04I9lOHmLOD/1QmU7BptOATsl2Gkrc+xDsyLDcg5KDI0+RrG7xVYospsWZ2FUFgi6L1bGNvKyYmzVaxqR3EYZ34g+j1U+URmKvYD1ogfdfBSkh/TZMe4GRJy5UWr9DRMp1NIDEgAgERaRZcFh6OCgo8rBgYjEiGfBi78T6H108+iiad6oVpQC74+nZdW5hgJ6l1DVrMyHWhdZ23/VK1Y8tPjgjDu25N64TD/hgBdthAzqGxP4vr0c+3klmhMkC/o7h2Pv/V2OR39YYJnWO0OqQiehKKDw0V5tVXepBN5lY9He0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H22WnTNarcWOpmDDXforESANan2+CLNA6CK7ERCRHNIW3cBTm1X1M9NXroN/TtM5W22mbxfGvexZkWAPgzsg0btl9io3vAheJJpSLuxixag0qIKVZE+9c94IdjLPiaKDIVa4jLFT9QkHHezE+U3EaIGZCaNEcAbtjjtPablMRKfokDVFO1qQ/AV6l7PAVGiIKi4B6zdHIvsoMsDx+7kJ4AfG/lngeCRo5eYeR2kfF1H2qMN++TUzJ+7slHA5vYGbfX0innvAPgBZlJlhJoCSWbVF28G6yMpGuRnSeKvaui41cLqdutK6zhSFTQ6a5YOP/xt265q33liSFtOWaWnvMtfESb8Z3YnHlMH9+7ROR9kAdmXknXZdPi2vlE0Nep5JgqI1QTmEt2uhPkPGyEnM9lyECqS6L2wJDStvstuvXzP2F+MLt5t+pkPfEBpVnR9H5VSO+jZ6Q79SiRH1TTnLQzO71dIW1pP0RpfpK6BZZylmFB1M0hpt1tyIHwCjDKFv5HIm3ztKLiH2hku0it3C7LxMydMUyla4su11UA2Yvs8azdiRxuFfWdyU1hPI3avIZrTlsvYL/uBOsN/9MvL13CX1lb0A9VIl5chGUrvWLPmzyPPZn35uwqj6ZfbsMkTpPPcAh9fvL6v5wE9qLV4OOIoJbPo6m9oPP2tBmh9X2jYQvw+bmy4lPf7dQz9c6oNjdMtNusnDnM1glRpzIt5Lm+YLurK/NgNXr20pTJoMiFnu71zHU6ulL0iADRWzElMCMGCSqGSIb3DQEJFTEWBBRdwtd+UNhstwu13AYfLbpScBAXfjBBMDEwDQYJYIZIAWUDBAIBBQAEIPenDrbzY8a4wZhWQ/YyjzMY9ZVRlWgST7OoYhL3tOxCBAhX/Fnkw1TfQgICCAA=";
    const myPkcs12ArrayBuffer = base64ToArrayBuffer(p12base64);
    cordova.plugin.http.setClientAuthMode('buffer', {
        rawPkcs: myPkcs12ArrayBuffer,
        pkcsPassword: 'abc830806'
    }, function(){
        document.getElementById("clientTrustInfo").innerHTML = "设置成功";
    }, function(errorInfo){
        document.getElementById("clientTrustInfo").innerHTML = "设置失败";
    });

}